<template>
  <div class="login-container">
    <el-card header="请先登录" class="login-card"> 
      <!-- native表示监听原生表单事件，prevent指阻止表单的默认提交不要跳转页面 -->
      <el-form @submit.native.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="model.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      model: {}
    }
  },
  methods: {
    async login() {  
      const res = await this.$http.post('login',this.model)
      // sessionStorage 表示浏览器的存储 关掉以后token就消失
      // sessionStorage.token = res.data.token
      // localStorage  表示浏览器的存储 关掉以后重新打开还有只要是同一个域名和网址
      localStorage.token = res.data.token
      this.$router.push('/')
      this.$message({
        type:'success',
        message:'登录成功'
      })
    }
  }
}
</script>

<style>
.login-card{
  width:25rem;
  margin:5rem auto; 
}
</style>